<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="queryDate.query" clearable @clear="getGoodList" placeholder="请输入内容" class="input-with-select">
            <el-button @click="getGoodList" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="5">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodList" style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" width="100px" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" width="100px" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" width="150px">
          <template slot-scope="scope">{{scope.row.add_time | datematfor}}</template>
        </el-table-column>
        <el-table-column width="200px" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="clearGoodsList(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryDate.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryDate.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryDate: {
        query: "",
        pagesize: 10,
        pagenum: 1
      },
      goodList: [],
      total: 0
    };
  },

  methods: {
    async getGoodList() {
      const res = await this.axios.get("goods/", {
        params: this.queryDate
      });
      if (res.meta.status !== 200) return;
      console.log(res);

      this.goodList = res.data.goods;
      this.total = res.data.total;
      console.log(this.total);
      
    },
    handleSizeChange(val) {
        this.queryDate.pagesize = val
        this.getGoodList()
    },
    handleCurrentChange (val) {
            this.queryDate.pagenum = val;
            this.getGoodList()
    },
    clearGoodsList (id) {
        this.$cfm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then( async() => {
            console.log(id); 
          const res = await this.axios.delete('goods/' + id)
          if(res.meta.status !== 200) return    this.$mg.error(res.meta.msg)
          this.getGoodList() 
          this.$mg.success(res.meta.msg)
        }).catch(() => {
          this.$mg.info('已经取消删除');
          
        });
    },
    addGoods() {
        this.$router.push('goods/add/')
    }
  },

  created() {
    this.getGoodList();
  }
};
</script>

<style lang='less' scoped>
</style>
